package cn.billy.org.controller;

import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.utils.ExcelUtil;
import cn.billy.basic.utils.LoginContext;
import cn.billy.org.domain.ShopAuditLog;
import cn.billy.org.service.IShopService;
import cn.billy.org.domain.Shop;
import cn.billy.org.query.ShopQuery;
import cn.billy.basic.utils.AjaxResult;
import cn.billy.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop getById(@PathVariable("id") Long id) {
        System.out.println("111111"+shopService.getById(id));
        return shopService.getById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> loadAll() {

        return shopService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList queryData(@RequestBody ShopQuery query, HttpServletRequest request) {
        Object loginUser = LoginContext.getLoginUser(request);
        System.out.println(loginUser);
        return shopService.queryData(query);
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            System.out.println("4444444444"+shop);
            // 店铺入驻，传入前端的数据
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }

    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            // 传入前端的数据
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            // 传入前端的数据
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }
    // audit/fail
    @PostMapping("/audit/fail")
    public AjaxResult fail(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            // 传入前端的数据
            shopService.fail(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }
    // /export/excel
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse resp){
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",resp);
    }

    // /import/excel
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file")MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        System.out.println(shops.size());
    }

    @GetMapping("/echarts")
    public Map<String,Object> loadShop(){
        return shopService.loadShop();
    }
}
